package com.kadron.embed;

import java.io.Serializable;

import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;

import com.kadron.common.KConst;
import com.kadron.dao.KEmbedDAO;
import com.kadron.datamodel.KDataModel;
import com.kadron.datamodel.KEmbedModelDataProvider;
import com.kadron.entity.Embed;
import com.kadron.manager.KMessageManager;
import com.kadron.manager.KSelection;

@AutoCreate
@Scope(ScopeType.PAGE)
@Name("kEmbedListManager")
public class KEmbedListManager extends KMessageManager implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3111988099407754250L;
	
	private KDataModel embedList;
	private KEmbedModelDataProvider dataProvider;
	
	@In
	private KEmbedDAO embedDAO;
	
	@Create
	public void init() {
		dataProvider = new KEmbedModelDataProvider(null, embedDAO);
		embedList = new KDataModel(dataProvider);
	}

	public KDataModel getEmbedList() {
		return embedList;
	}
	
	public String edit(Long id) {
		if (id == null) {
			bad(KConst.BAD_THING);
			return null;
		}
		KSelection kselection = (KSelection) Component.getInstance("kSelection");
		kselection.setItemId(id);
		return "e_embed";
	}
	
	public void delete(Embed embed) {
		if (embed == null) {
			bad(KConst.BAD_THING);
			return;
		}
		embedList.remove(embed);
		embedDAO.delete(embed);
	}
	
	public void ca(Embed embed) {
		if (embed == null) {
			bad(KConst.BAD_THING);
			return;
		}
		embed.setActive(!embed.isActive());
		embed = (Embed) embedDAO.update(embed);
	}
}
